const { log } = require('debug/src/browser');
var express = require('express');
var router = express.Router();

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// web3.eth.getAccounts().then(console.log);

var myContract = new web3.eth.Contract([
	{
		"inputs": [],
		"stateMutability": "nonpayable",
		"type": "constructor"
	},
	{
		"inputs": [],
		"name": "getCounter",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "step",
				"type": "uint256"
			}
		],
		"name": "kipCounter",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
], '0x949F71e6ea971365EBD676D0F922160bFf5ad6F4');

/* myContract.methods.getCounter().call({from: '0x1b86E4CB76E8300BB14152Af05103c4267A53570'}, function(error, result){
  console.log('counter: ', result)
}); */

var init_counter = 0;
var current_counter = 0;
myContract.methods.getCounter().call().then(function(counter) {
  console.log('init counter', counter);
  init_counter = counter;
});


/* GET home page. */
router.get('/', function(req, res, next) {
  myContract.methods.kipCounter(5).send({from: '0x1b86E4CB76E8300BB14152Af05103c4267A53570'})
  .then(function(){
    myContract.methods.getCounter().call().then(function(counter) {
      console.log('current counter', counter);
      current_counter = counter;
      res.render('index', { init: init_counter, current: current_counter });
    });
  });
});

module.exports = router;
